﻿$(document).ready(function(){
    LoadMarkups();
});

function LoadMarkups(){
    
}

function LoadMarkup(control){
    $('#'+control).markItUp(mySettings);
}

function LinkMenu() {

    this._listTemplate = "<a href='{1}{2}/{3}' class='vertical-menu'>{0}</a>";
    this._listTemplateRed = "<a href='{1}{2}/{3}' class='vertical-menu' style='color:Red'>{0}</a>";
    this._hoverItem;
    
    this.NotifyConsole = function(message)
    {
        try{
            console.log( message );
        }catch(e){
        
        }
    };

    this.Render = function(control, hoverItem, requestUrl, requestPath, createLink, createText) {

        this.NotifyConsole(String.format("Call for control:[{0}] hoverItem:[{1}] req:[{2}]",
            control, hoverItem, requestUrl));
        template = this._listTemplate;
        templateRed = this._listTemplateRed;
        //if the don't use the keyword var , somehow , the javascript get screwed , 
        // and adds the data into the 2nd instance's container.

        var controlRef = "#" + control;

        $.getJSON(requestUrl, function(data) {
            html = "";
            if (createLink) {

                html += String.format(templateRed, createText, serverBase, createLink, "");
            }
            $.each(data, function(i, item) {
                html += String.format(template, item.Name, serverBase, requestPath, item.FriendlyUrl);
            });

            $(controlRef).append(html);
        });

        this.AddEventsToListItem("#" + hoverItem);
    };

    this.AddEventsToListItem = function(hoverItem) {
        var mouseOver = this.OnListMouseOver;
        var mouseOut = this.OnListMouseOut;
        $(hoverItem).hover(function() { mouseOver(this); }, function() { mouseOut(this); });
        
    };

    this.OnListMouseOver = function(control) {
        $(control).children('.MenuContainer').css({ display: 'inline', position: 'absolute', zIndex: 5000 });
    };
    this.OnListMouseOut = function(control) {
        $(control).children('.MenuContainer').css({ display: 'none' });
    };
}

String.format = function(text) {
    if (arguments.length <= 1) {
        return text;
    }
    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for (var token = 0; token <= tokenCount; token++) {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"), arguments[token + 1]);
    }
    return text;
};


function PostWebService() {
    
    $.post(serverBase + "/Expression/", {
        source: '{Code: http://wikey.googlecode.com/svn/trunk/Wikey.Transformation.Tests/ExpressionEvaluatorTests.cs}',
        type: 'json'
    }, function(response) {
        html = '<div>' + response + '</div>';
        $('#hidden').append(html);
    }
    );
}